package org.lalib.controller.action;

import java.util.List;

import org.lalib.acquire.IControllerAction;
import org.lalib.controller.form.CUserGroupForm;
import org.lalib.controller.service.CUserGroupService;
import org.lalib.entity.object.EUserGroup;
import org.lalib.mediator.MUserGroup;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Service;

@Scope("request")
@Service(value = "cUserGroupAction")
public class CUserGroupAction implements IControllerAction {

	private static final long serialVersionUID = 1L;

	@Autowired
	private CUserGroupForm cUserGroupForm;

	@Autowired
	private MUserGroup mUserGroup;

	@Autowired
	private CUserGroupService cUserGroupService;

	public String init() {
		List<EUserGroup> userGroupList = cUserGroupService.getAllUserGroups();
		cUserGroupForm.setUserGroupList(userGroupList);
		setNewUserGroup();
		return "userGroupDefination";
	}

	// @PostConstruct
	public void loadList() {
		List<EUserGroup> userGroupList = cUserGroupService.getAllUserGroups();
		cUserGroupForm.setUserGroupList(userGroupList);
		setNewUserGroup();
	}

	public void setNewUserGroup() {
		cUserGroupForm.setUserGroup(new EUserGroup());
	}

	public void insert() {
		EUserGroup userGroup = cUserGroupForm.getUserGroup();
		cUserGroupService.insert(userGroup);
		loadList();
	}

	public void setUserGroupForUpdate(EUserGroup userGroup) {
		cUserGroupForm.setEnableUpdate(true);
		cUserGroupForm.setUserGroup(userGroup);
	}

	public void update() {
		EUserGroup userGroup = cUserGroupForm.getUserGroup();
		cUserGroupService.update(userGroup);
		cUserGroupForm.setEnableUpdate(false);
		loadList();
	}

	public void deleteUserGroup(EUserGroup userGroup) {
		cUserGroupForm.setUserGroup(userGroup);
	}

	public void delete() {
		EUserGroup userGroup = cUserGroupForm.getUserGroup();
		cUserGroupService.remove(userGroup);
		loadList();
	}

	public String cancel() {
		return null;
	}
}